﻿using Microsoft.IdentityModel.Tokens;
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using System.Text.RegularExpressions;

namespace FirstDemo.Common
{
    public class Utils
    {
        /// <summary>
        /// 获得浏览器的类型及版本
        /// </summary>
        /// <param name="userAgent">UserAgent的值</param>
        /// <param name="browserName">浏览器类型</param>
        /// <param name="ver">浏览器版本</param>
        /// <returns></returns>
        public static string GetBrowserName(string userAgent, out string browserName, out string ver)
        {
            string fullBrowserName = string.Empty;
            browserName = string.Empty;
            ver = string.Empty;
            // IE
            string regexStr = @"msie (?<ver>[\d.]+)";
            Regex r = new Regex(regexStr, RegexOptions.IgnoreCase);
            Match m = r.Match(userAgent);
            if (m.Success)
            {
                browserName = "IE";
                ver = m.Groups["ver"].Value;
                fullBrowserName = string.Format("{0} {1}", browserName, ver);
                return fullBrowserName;
            }
            // Firefox
            regexStr = @"firefox\/([\d.]+)";
            r = new Regex(regexStr, RegexOptions.IgnoreCase);
            m = r.Match(userAgent);
            if (m.Success)
            {
                browserName = "firefox";
                ver = m.Groups["ver"].Value;
                fullBrowserName = string.Format("{0} {1}", browserName, ver);
                return fullBrowserName;
            }
            // Chrome
            regexStr = @"chrome\/([\d.]+)";
            r = new Regex(regexStr, RegexOptions.IgnoreCase);
            m = r.Match(userAgent);
            if (m.Success)
            {
                browserName = "chrome";
                ver = m.Groups["ver"].Value;
                fullBrowserName = string.Format("{0} {1}", browserName, ver);
                return fullBrowserName;
            }
            // Opera
            regexStr = @"opera.([\d.]+)";
            r = new Regex(regexStr, RegexOptions.IgnoreCase);
            m = r.Match(userAgent);
            if (m.Success)
            {
                browserName = "opera";
                ver = m.Groups["ver"].Value;
                fullBrowserName = string.Format("{0} {1}", browserName, ver);
                return fullBrowserName;
            }
            // Safari
            regexStr = @"version\/([\d.]+).*safari";
            r = new Regex(regexStr, RegexOptions.IgnoreCase);
            m = r.Match(userAgent);
            if (m.Success)
            {
                browserName = "safari";
                ver = m.Groups["ver"].Value;
                fullBrowserName = string.Format("{0} {1}", browserName, ver);
                return fullBrowserName;
            }
            return fullBrowserName;
        }

        /// <summary>
        /// 获取Token方法
        /// </summary>
        /// <returns>返回Token字符串</returns>
        public static string GetToken(string username, string userId)
        {
            var claim = new Claim[]
            {
                new Claim(ClaimTypes.Name,username),
                new Claim(ClaimTypes.NameIdentifier, userId),
                //new Claim(JwtRegisteredClaimNames.Jti,"1"),//编号
                new Claim(JwtRegisteredClaimNames.Iat, $"{new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds()}"),//签发时间
                new Claim(JwtRegisteredClaimNames.Nbf,$"{new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds()}"),//生效时间
                // 过期时间 100秒
                //new Claim(JwtRegisteredClaimNames.Exp,$"{new DateTimeOffset(DateTime.Now.AddSeconds(100)).ToUnixTimeSeconds()}"),
                //new Claim(JwtRegisteredClaimNames.Iss,"Lesan"), // 签发者
                //new Claim(JwtRegisteredClaimNames.Aud,"User") // 接收者
            };
            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("EF1DA5B7-C8FA-8280-B997-7D1701BF9BE2"));
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            var token = new JwtSecurityToken(
                issuer: "Lesan",
                audience: "User",
                claims: claim,
                notBefore: DateTime.Now,
                expires: DateTime.Now.AddMinutes(30),
                signingCredentials: creds);
            return new JwtSecurityTokenHandler().WriteToken(token);
        }
    }
}
